﻿
'Copyright 2010 Brian L. Crawford

'Licensed under the Apache License, Version 2.0 (the "License");
'you may not use this file except in compliance with the License.
'You may obtain a copy of the License at

'    http://www.apache.org/licenses/LICENSE-2.0

'Unless required by applicable law or agreed to in writing, software
'distributed under the License is distributed on an "AS IS" BASIS,
'WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
'See the License for the specific language governing permissions and
'limitations under the License.

'This software uses Microsoft .Net framework used with permission from      
'Microsoft. Microsoft, .Net Framework, Visual Studio, Visual Basic, Visual C++ 
'remain the trademark and copyright of Microsoft and are subject to their 
'individual licenses.

Imports System.Collections.ObjectModel
Imports System.IO
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.IO.Directory
Public Module COMPAREFUNCTIONS



    Public Function CompareFiles(ByRef List1 As ListBox, ByRef List2 As ListBox, ByVal counter As Integer)

        'this is one big hack. but it is fast....at least for small amount of data
        'making a sorted binary tree didn't make sense.  becuase i don't want to spend the time building and arranging
        'it just for a quick comparison that we only use once.
        If counter < 0 Then End
        Dim arrtemp As Object = Nothing
        Dim itemTemp As Object = Nothing
        'start at the end of the list
        Do While counter > 0
            For Each item In List1.Items
                'if you remove items for the collection while in this loop the enumerator will throw an 
                'exception becuase the scope will have changed and it doesn't have a way to deal with
                'dynamic collections
                If StrComp(item.displayname, List2.Items.Item(counter).displayname, CompareMethod.Text) = 0 Then
                    'the order matters because i'm ref-ing the item in list2
                    arrtemp = List2.Items.Item(counter)
                    itemTemp = item
                End If
            Next
            'i could remove the item from List1 here. 
            If TypeOf arrtemp Is classListboxObject = True Then
                List1.Items.Remove(itemTemp)
                List2.Items.Remove(arrtemp)
            End If
            counter -= 1
        Loop
        'compare the first item
        If StrComp(List1.Items.Item(counter).displayname, List2.Items.Item(counter).displayname, CompareMethod.Text) = 0 Then
            'the order matters because i'm ref-ing the item in list2
            List1.Items.Remove(List2.Items.Item(counter))
            List2.Items.Remove(List2.Items.Item(counter))
        End If


    End Function

    Sub WriteLog(ByVal n As String, ByVal folder As String, Optional ByVal PATH As String = Nothing)
        If folder = Nothing Then Exit Sub 'add message somewhere to say "Nothing to save"
        Dim dir As DirectoryInfo
        dir = Directory.GetParent(folder)
        Dim file As New SaveFileDialog
        file.Filter() = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
        file.DefaultExt() = "txt"
        file.AddExtension() = True
        file.FileName() = "compare of " & dir.Name
        file.Title() = "Save the comparison report..."
        file.InitialDirectory() = dir.Name
        If file.ShowDialog = DialogResult.OK Then
            Dim sw As StreamWriter = New StreamWriter(file.FileName)
            ' Add some text to the file.
            sw.Write(n)
            sw.Close()

        End If
    End Sub

    Sub WriteStrings(ByVal n As String, ByVal PATH As String)
        Dim c As String
        For Each c In n
            Call WriteLog(c, PATH)
        Next
    End Sub

    Function CheckIfFileExists(ByVal FilePathName As String) As Boolean
        'check to see if the file exists - use fileinfo methods
        Dim MyFilePath As New FileInfo(FilePathName)
        Return MyFilePath.Exists()
    End Function

    'FIX THE COLLECTION FUNCTIONS - DON'T USE WINDOWS SCRIPTING
    Function collect_subfolders(ByRef Directory As DirectoryInfo)

        Return Directory.GetDirectories()
    End Function

    Function collect_subfiles(ByRef folderpath As String, ByVal worker As BackgroundWorker, ByVal e As DoWorkEventArgs, ByRef getSubfiles As Boolean) As Long
        Try
            Dim MyDirectory As New DirectoryInfo(folderpath)
            collect_files(MyDirectory, worker, e)
            If getSubfiles = True Then
                Dim subfolders As Array
                subfolders = collect_subfolders(MyDirectory)
                For Each item In subfolders
                    If worker.CancellationPending = True Then Exit Function
                    collect_files(item, worker, e)
                    collect_subfiles(item.FullName, worker, e, getSubfiles)
                Next
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString)
        End Try
        Return True
    End Function

    Sub collect_files(ByVal directory As DirectoryInfo, ByVal worker As BackgroundWorker, ByVal e As DoWorkEventArgs)

        Dim files As FileInfo() = directory.GetFiles()
        Dim file As FileInfo
        For Each file In files
            worker.ReportProgress(0, file)
        Next
    End Sub
    Sub fileAddtoCollection(ByRef listbox As ListBox, ByVal file As FileInfo)

        If listbox.Items.Contains(file) = False Then
            listbox.Items.Add(file)
            listbox.SetSelected(listbox.Items.Count - 1, True)
            listbox.SetSelected(listbox.Items.Count - 1, False)

        End If
    End Sub
    Sub fileAddtoCollection(ByRef listbox As ListBox, ByVal file As String)

        If listbox.Items.Contains(file) = False Then
            listbox.Items.Add(file)
            listbox.SetSelected(listbox.Items.Count - 1, True)
            listbox.SetSelected(listbox.Items.Count - 1, False)


        End If
    End Sub
    Sub fileAddtoCollection(ByRef listcol As ListBox, ByVal file As classListboxObject)
        'methods to add file object with display memeber as string to listbox
        'these value below need to go elsewhere.
        listcol.DisplayMember = "DisplayName"
        listcol.ValueMember = "file"
        If listcol.Items.Contains(file.DisplayName) = False Then 'DO SOMETHING ABOUT DUPLICATE OBJECTS
            listcol.Items.Add(file)
            listcol.SetSelected(listcol.Items.Count - 1, True)
            listcol.SetSelected(listcol.Items.Count - 1, False)
        End If
    End Sub


End Module